
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import db.DataVO;
import db.ExeSQL;

public class Test extends JFrame implements ActionListener {
	
	/****/
	private static final long serialVersionUID = 1L;
	
	private JScrollPane panel;
	private JButton next,previous,add,delete;
	private JLabel label1;
	private PageCtrTable table;
	
	/**
	 * 测试程序入口
	 * @param args
	 */
	public static void main(String[] args) {
		new Test();
	}
	
	public Test(){
		super("JTable Page Controlls");
		
		ListVo list = new ListVo();
		
		initTableData(list.getBindList());
		initComponent();
	}
	
	/**
	 * 添加数据源
	 */
	public void initTableData(List<DataVO> list) {
		PageCtrTable.BindData(list);
	}
	
	/**
	 * 组件初始化
	 */
	private void initComponent() {
		this.setSize(500,230);
		
		table=new PageCtrTable();
		// 设置JTable只能一次选中一行
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		// 程序启动时，默认选中第一行
		if (table.getRowCount() > 0) {
			table.setRowSelectionInterval(0,0);
		}
		
		panel=new JScrollPane(table);		
		panel.setBounds(10, 10, 470, 119);
		
		previous=new JButton("上一页");
		previous.setBounds(180, 150, 75,20);
		
		next=new JButton("下一页");
		next.setBounds(255, 150, 75, 20);
		
		add=new JButton("添加");
		add.setBounds(350, 150, 65, 20);
		
		delete=new JButton("删除");
		delete.setBounds(420, 150, 65, 20);
		
		previous.addActionListener(this);
		next.addActionListener(this);
		add.addActionListener(this);
		delete.addActionListener(this);
		
		label1=new JLabel( "第" + table.currentPage + "页 / 共" +  table.getTotalPage() + "页" );
		label1.setBounds(10, 150, 150, 20);
		
		this.getContentPane().setLayout(null);
		this.getContentPane().add(panel);
		this.getContentPane().add(previous);
		this.getContentPane().add(next);
		this.getContentPane().add(add);
		this.getContentPane().add(delete);
		this.getContentPane().add(label1);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	/**
	 * 按钮事件
	 */
	public void actionPerformed(ActionEvent e) {
		JButton button=(JButton) e.getSource();
		if(button.equals(previous)){
			int i=table.getPreviousPage();
			if(i==-1)
				return;
		}
		
		if(button.equals(next)){
			int i=table.getNextPage();
			if(i==-1)
				return;
		}
		
		if(button.equals(delete)){
			int i=table.getSelectedRow();
			if(i==-1)
				return ;
			Integer id=(Integer) table.getValueAt(i,0);
			if(id==null)return ;
			DataVO s=null;
			for(DataVO stu:PageCtrTable.getBindData()){
				if(stu.getId() == id)
					s=stu;
			}
			int index=PageCtrTable.getBindData().indexOf(s);
			
			System.out.println(s.getId());
			// 执行删除
			ExeSQL.deleteDataVO(s.getId());
			
			PageCtrTable.getBindData().remove(index);
			table.initTable();
			label1.setText( "第" + table.currentPage + "页 / 共" +  table.getTotalPage() + "页" );
			return;
		}
		
		if(button.equals(add)){
			Integer id=0;
			for(DataVO stu:PageCtrTable.getBindData()){
				if(stu.getId()>id)id=stu.getId();
			}
			DataVO dataVO=new DataVO(id+1,"黄浪"+(id+1),22,0);
			
			// 插入一条数据
			ExeSQL.addDataVO(dataVO);
			
			PageCtrTable.getBindData().add(dataVO);
			
			table.initTable();
			
			label1.setText( "第" + table.currentPage + "页 / 共" +  table.getTotalPage() + "页" );
			return;
		}
		DefaultTableModel model=new DefaultTableModel(table.getPageData(),table.columnNames);
		table.setModel(model);
		label1.setText( "第" + table.currentPage + "页 / 共" +  table.getTotalPage() + "页" );
	}

}